package com.teamone.etl.accountant.controller;

import com.alipay.api.AlipayApiException;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.github.pagehelper.PageInfo;
import com.teamone.etl.accountant.dto.InvoicingDto;
import com.teamone.etl.accountant.dto.RechargeDto;
import com.teamone.etl.accountant.entity.InvoiceSpecification;
import com.teamone.etl.accountant.entity.PersonalInformation;
import com.teamone.etl.accountant.entity.RechargeSheet;
import com.teamone.etl.accountant.service.InvoiceSpecificationService;
import com.teamone.etl.accountant.service.PersonalInformationService;
import com.teamone.etl.accountant.service.RechargeSheetService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @ClassName: ExpenceController
 * @Author zhaopeijie
 * @Date 2021/1/20
 * 费用管理模块 ------ 使用 控制层
 */
@RestController
@RequestMapping("expence")
@CrossOrigin
public class ExpenceController extends ApiController {
    @Resource
    private PersonalInformationService personalInformationService;

    @Resource
    private RechargeSheetService rechargeSheetService;

    @Resource
    private InvoiceSpecificationService invoiceSpecificationService;

    @GetMapping("queryById/{id}")
    public R queryById(@PathVariable("id") Integer id){
        return success(personalInformationService.queryById(id));
    }
    /**
     * 充值
     */
    @PostMapping("recharge")
    public R<Boolean> recharge(@RequestBody RechargeDto rechargeDto) throws AlipayApiException {
        RechargeSheet rechargeSheet = new RechargeSheet();
        BeanUtils.copyProperties(rechargeDto,rechargeSheet);
        return success(rechargeSheetService.recharge(rechargeSheet)>0);
    }
    /**
     * 充值明细
     */
    @PostMapping("rechargeSheetList")
    public R rechargeSheetList(@RequestParam(value = "accountId",required = true) Integer accountId,
                                                 @RequestParam(value = "rechargeTime",required = false) String rechargeTime,
                                                 @RequestParam(value = "page",defaultValue = "1") Integer page,
                                                 @RequestParam(value = "size",defaultValue = "10") Integer size){
       PageInfo<RechargeSheet> search = rechargeSheetService.rechargeSheetList(accountId,rechargeTime,page,size);
       return success(search);
    }

    /**
     * 导入excel
     * @return
     */
    @PostMapping("importRechargeSheetExcel")
    public R importExcel(@RequestParam("accountId") Integer accountId){
        List<RechargeSheet> list = rechargeSheetService.findById(accountId);
        if(list.size()>0){
            rechargeSheetService.easyExcel(accountId);
            return success(true);
        }else {
            return success(false);
        }
    }
    /**
     * 开启预警
     */
    @PostMapping("openWarning")
    public R openWarning(@RequestParam(value = "id",required = true) Integer id,
                         @RequestParam(value = "warningThreshold",required = true) BigDecimal warningThreshold,
                         @RequestParam(value = "warningPhone",required = true) String warningPhone){
        return success(personalInformationService.openWarning(id,warningThreshold,warningPhone));
    }
    /**
     * 关闭预警
     */
    @PostMapping("closeWarning")
    public R closeWarning(@RequestParam(value = "id",required = true) Integer id){
        return success(personalInformationService.closeWarning(id));
    }


    /**
     * 开发票
     */
    @PostMapping("invoicing")
    public R invoicing(@RequestBody InvoicingDto invoicingDto){
        return success(invoiceSpecificationService.invoicing(invoicingDto)>0);
    }


    /**
     * 发票记录
     */
    @PostMapping("invoiceList")
    public R invoiceList(@RequestParam(value = "enterpriseId",required = true) Integer enterpriseId,
                               @RequestParam(value = "invoicereqTime",required = false) String invoicereqTime,
                               @RequestParam(value = "invoicerStatus",required = false) Integer invoiceStatus,
                               @RequestParam(value = "invoiceType",required = false) Integer invoiceType,
                               @RequestParam(value = "page",defaultValue = "1") Integer page,
                               @RequestParam(value = "size",defaultValue = "10") Integer size){
        PageInfo<InvoiceSpecification> search = invoiceSpecificationService.invoiceList(enterpriseId,invoicereqTime,invoiceStatus,invoiceType,page,size);
        return success(search);
    }

    /**
     * 导入excel
     * @return
     */
    @PostMapping("importInvoiceExcel")
    public R importInvoiceExcel(@RequestParam("enterpriseId") Integer enterpriseId){
        List<InvoiceSpecification> list = invoiceSpecificationService.findById(enterpriseId);
        if(list.size()>0){
            invoiceSpecificationService.easyExcel(enterpriseId);
            return success(true);
        }else {
            return success(false);
        }
    }


    @PostMapping("personalInformation/{id}")
    public R personalInformation(@PathVariable("id") Integer id){
        PersonalInformation personalInformation = personalInformationService.personalInformation(id);
        if(personalInformation!=null){
            return success(personalInformation);
        }else {
            return success(false);
        }
    }
}
